package com.secondmybatis.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;

import java.io.IOException;
@WebFilter(urlPatterns = "/*")  //   "/*"  拦截所有网站请求
//@WebFilter(urlPatterns = "/login")   // 拦截具体路径
//@WebFilter(urlPatterns = "/emps/*")   // 拦截具体目录下得所有路径
public class DemoFilter implements Filter {
    // 1.初始化方法,在Web服务器启动时触发一次
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Demo Filter init>>>>>>>>>>>");
        Filter.super.init(filterConfig);
    }
    // 销毁方法，在web服务器关闭时触发一次
    @Override
    public void destroy() {
        System.out.println("Demo Filter destroy>>>>>>>>>>>");
        Filter.super.destroy();
    }

    // 每次拦截到请求，就会触发该方法，会调用多次
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 拦截放行
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
